/**
 * @file 账单管理 过滤器
 * @author lishaohai
 */

import {
    BILL_STATUS_REPAYMENT,
    BILL_STATUS_SETTLED,
    BILL_STATUS_PAST_DUE,
    getDefaultCashs,
    BILL_STATUS_TEXT_REPAYMENT,
    BILL_STATUS_TEXT_SETTLED,
    BILL_STATUS_TEXT_PAST_DUE
} from '@/views/bill/constant-bill';

/**
 * @description 根据分期账单数据，补全没有的数据
 * @param {Array} values 后端返回的分期账单数据
 * @returns 返回补全的数据
 */
export function handleCompleteBillRecords(values) {
    const defaultValues = getDefaultCashs();
    return defaultValues.map(item => {
        const index = values.findIndex(cValue => item._id === cValue._id);
        return index !== -1 ? {...item, ...values[index]} : item;
    });
}

/**
 * @description 根据账单状态码返回对应的文案
 * @param {number} value 账单状态码
 * @returns {string} 还款中、已结清、已逾期
 */
export function handleGetBillStatusTextByStatus(value) {
    let output;
    switch (value) {
        case BILL_STATUS_REPAYMENT: {
            output = BILL_STATUS_TEXT_REPAYMENT;
            break;
        }
        case BILL_STATUS_SETTLED: {
            output = BILL_STATUS_TEXT_SETTLED;
            break;
        }
        case BILL_STATUS_PAST_DUE: {
            output = BILL_STATUS_TEXT_PAST_DUE;
            break;
        }
        default:
            output = '';
            break;
    }
    return output;
}
/**
 * @description 根据账单状态码返回对应的文案
 * @param {number} value 账单状态码
 * @returns {string} 还款中、已结清、已逾期
 */
export function handleGetBillStatusByText(value) {
    let output;
    switch (value) {
        case BILL_STATUS_TEXT_REPAYMENT: {
            output = BILL_STATUS_REPAYMENT;
            break;
        }
        case BILL_STATUS_TEXT_SETTLED: {
            output = BILL_STATUS_SETTLED;
            break;
        }
        case BILL_STATUS_TEXT_PAST_DUE: {
            output = BILL_STATUS_PAST_DUE;
            break;
        }
        default:
            output = BILL_STATUS_TEXT_REPAYMENT;
            break;
    }
    return output;
}

/**
 * @description 根据账单分期详情返回账单状态
 * @param {Array} values 账单详情
 * @returns {number} 账单状态
 */
export function handleGetTheBillTotalStatus(values) {
    let newItem;
    // 已逾期
    const pastDueIndex = values.findIndex(stageItem => stageItem.status === BILL_STATUS_PAST_DUE);

    // 还款中
    let repaymentIndex = -1;

    if (pastDueIndex === -1) {
        repaymentIndex = values.findIndex(stageItem => stageItem.status === BILL_STATUS_REPAYMENT);
    }

    if (pastDueIndex !== -1) {
        newItem = {...values[pastDueIndex]};
    } else if (pastDueIndex === -1 && repaymentIndex !== -1) {
        newItem = {...values[repaymentIndex]};
    } else {
        // 已结清
        newItem = {...values[values.length - 1]};
    }

    return newItem;
}

/**
 * @description 根据账单状态码 返回classname 状态类名
 * @param {number} status 账单状态码
 * @returns {string} classname
 */
export function handleGetTheBillClassnameByTotalStatus(status) {
    const newItem = {
        [BILL_STATUS_REPAYMENT]: 'is-info',
        [BILL_STATUS_SETTLED]: 'is-success',
        [BILL_STATUS_PAST_DUE]: 'is-danger'
    };
    return newItem[status] ? newItem[status] : '';
}

/**
 * @description 根据账单状态，判断是否已完成
 * @param {number} status 账单状态码
 * @returns {boolean} 是否已结清
 */
export function isBillCompletedByStatus(value) {
    return value === BILL_STATUS_SETTLED;
}
